package com.neusoft.weekreport.component.bizService.impl;

import com.neusoft.weekreport.component.bizService.DayBizService;
import com.neusoft.weekreport.component.entities.BsDayDetail;
import com.neusoft.weekreport.component.entities.BsDayDetailExample;
import com.neusoft.weekreport.component.req.*;
import com.neusoft.weekreport.component.resp.GetDayDetailInfoResp;
import com.neusoft.weekreport.mapper.BsDayDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;


/**
 * @Author 张峰伟
 * @Create 2020/2/15 16:21
 */

@Service
public class DayBizServiceimpl implements DayBizService {

    @Resource
    BsDayDetailMapper bsDayDetailMapper;


    //保存日报明细
    @Override
    @Transactional
    public void saveDayDetailList(GetAddProductWorkReq req) throws Exception {

        String userId = req.getUserId();
        String dayId = req.getDayId();
        String groupId = req.getGroupId();
        String productType = req.getProductType();
        List<GetAddDayDetailReq> dayDetailList = req.getDayDetailList();
        BsDayDetail bsDayDetail = new BsDayDetail();

        for (GetAddDayDetailReq dayDetail : dayDetailList) {
            bsDayDetail.setColid(UUID.randomUUID().toString());
            DayTaskInfo dayTaskInfo = dayDetail.getTaskInfo();
            bsDayDetail.setTaskId(dayTaskInfo.getCode());
            String NewTaskId = dayTaskInfo.getCode();
            bsDayDetail.setId(UUID.randomUUID().toString());
            bsDayDetail.setDayId(dayId);
            bsDayDetail.setGroupId(groupId);
            bsDayDetail.setProductType(productType);
            bsDayDetail.setWorkContent(filterWorkContent(dayDetail.getWorkContent()));
            if(!StringUtils.isEmpty(dayDetail.getColid())) {//删除原用
                BsDayDetailExample bsDayDetailExample = new BsDayDetailExample();
                BsDayDetailExample.Criteria criteria = bsDayDetailExample.createCriteria();
                criteria.andColidEqualTo(dayDetail.getColid());
                criteria.andDayidEqualTo(dayId);
                List<BsDayDetail> bsDayDetailList = bsDayDetailMapper.selectByExample(bsDayDetailExample);
                String taskId="";
                for (BsDayDetail detail : bsDayDetailList) {
                    taskId = detail.getTaskId();
                    break;
                }
                bsDayDetailMapper.deleteByExample(bsDayDetailExample);
                if(!NewTaskId.equals(taskId)){//如果修改的是任务，则判断是否删除日报概述
                    //判断是否删除日报概述
                    GetDeleteDayReq getDeleteDayReq = new GetDeleteDayReq();
                    getDeleteDayReq.setDayId(req.getDayId());
                    getDeleteDayReq.setTaskId(taskId);
                }
                DayOfficeSpaceInfo officeSpaceInfo = dayDetail.getOfficeSpaceInfo();
                bsDayDetail.setOfficeSpace(officeSpaceInfo.getCode());
                DayTaskTypeInfo dayTaskTypeInfo = dayDetail.getTaskTypeInfo();
                bsDayDetail.setTaskType(dayTaskTypeInfo.getCode());
                bsDayDetail.setDeliverables(dayDetail.getDeliverables());
                bsDayDetail.setLaborHour(dayDetail.getLaborHour());
                bsDayDetail.setStatesFlag(dayDetail.getStatesFlag());
                bsDayDetail.setCreateBy(userId);
                bsDayDetail.setCreateDate(sysDateTime());
                bsDayDetail.setUpdateBy(userId);
                bsDayDetail.setUpdateDate(sysDateTime());
            }
            DayOfficeSpaceInfo officeSpaceInfo = dayDetail.getOfficeSpaceInfo();
            bsDayDetail.setOfficeSpace(officeSpaceInfo.getCode());
            DayTaskTypeInfo dayTaskTypeInfo = dayDetail.getTaskTypeInfo();
            bsDayDetail.setTaskType(dayTaskTypeInfo.getCode());
            bsDayDetail.setDeliverables(dayDetail.getDeliverables());
            bsDayDetail.setLaborHour(dayDetail.getLaborHour());
            bsDayDetail.setStatesFlag(dayDetail.getStatesFlag());
            bsDayDetail.setCreateBy(userId);
            bsDayDetail.setCreateDate(sysDateTime());
            bsDayDetail.setUpdateBy(userId);
            bsDayDetail.setUpdateDate(sysDateTime());
            bsDayDetailMapper.insert(bsDayDetail);
        }


    }

    //添加日报明细
    @Override
    @Transactional
    public void addDayDetailList(GetAddProductWorkReq req) throws Exception {

        String userId = req.getUserId();
        String dayId = req.getDayId();
        String groupId = req.getGroupId();
        String productType = req.getProductType();
        List<GetAddDayDetailReq> dayDetailList = req.getDayDetailList();
        BsDayDetail bsDayDetail = new BsDayDetail();

        for (GetAddDayDetailReq dayDetail : dayDetailList) {
            bsDayDetail.setColid(UUID.randomUUID().toString());
            DayTaskInfo dayTaskInfo = dayDetail.getTaskInfo();
            bsDayDetail.setTaskId(dayTaskInfo.getCode());
            String NewTaskId = dayTaskInfo.getCode();
            bsDayDetail.setId(UUID.randomUUID().toString());
            bsDayDetail.setDayId(dayId);
            bsDayDetail.setGroupId(groupId);
            bsDayDetail.setProductType(productType);
            bsDayDetail.setWorkContent(filterWorkContent(dayDetail.getWorkContent()));
            if(!StringUtils.isEmpty(dayDetail.getColid())) {//删除原用
                BsDayDetailExample bsDayDetailExample = new BsDayDetailExample();
                BsDayDetailExample.Criteria criteria = bsDayDetailExample.createCriteria();
                criteria.andColidEqualTo(dayDetail.getColid());
                criteria.andDayidEqualTo(dayId);
                List<BsDayDetail> bsDayDetailList = bsDayDetailMapper.selectByExample(bsDayDetailExample);
                String taskId="";
                for (BsDayDetail detail : bsDayDetailList) {
                    taskId = detail.getTaskId();
                    break;
                }
                bsDayDetailMapper.deleteByExample(bsDayDetailExample);
                if(!NewTaskId.equals(taskId)){
                    GetDeleteDayReq getDeleteDayReq = new GetDeleteDayReq();
                    getDeleteDayReq.setDayId(req.getDayId());
                    getDeleteDayReq.setTaskId(taskId);
                }
                DayOfficeSpaceInfo officeSpaceInfo = dayDetail.getOfficeSpaceInfo();
                bsDayDetail.setOfficeSpace(officeSpaceInfo.getCode());
                DayTaskTypeInfo dayTaskTypeInfo = dayDetail.getTaskTypeInfo();
                bsDayDetail.setTaskType(dayTaskTypeInfo.getCode());
                bsDayDetail.setDeliverables(dayDetail.getDeliverables());
                bsDayDetail.setLaborHour(dayDetail.getLaborHour());
                bsDayDetail.setStatesFlag("2");
                bsDayDetail.setCreateBy(userId);
                bsDayDetail.setCreateDate(sysDateTime());
                bsDayDetail.setUpdateBy(userId);
                bsDayDetail.setUpdateDate(sysDateTime());
            }
            DayOfficeSpaceInfo officeSpaceInfo = dayDetail.getOfficeSpaceInfo();
            bsDayDetail.setOfficeSpace(officeSpaceInfo.getCode());
            DayTaskTypeInfo dayTaskTypeInfo = dayDetail.getTaskTypeInfo();
            bsDayDetail.setTaskType(dayTaskTypeInfo.getCode());
            bsDayDetail.setDeliverables(dayDetail.getDeliverables());
            bsDayDetail.setLaborHour(dayDetail.getLaborHour());
            bsDayDetail.setStatesFlag("2");
            bsDayDetail.setCreateBy(userId);
            bsDayDetail.setCreateDate(sysDateTime());
            bsDayDetail.setUpdateBy(userId);
            bsDayDetail.setUpdateDate(sysDateTime());
            bsDayDetailMapper.insert(bsDayDetail);
        }

    }

    //查询日报明细
    @Override
    public GetDayDetailInfoResp getDayDetailInfo(GetDayDetailReq req) throws Exception {
        GetDayDetailInfoResp resp = new GetDayDetailInfoResp();
        String userId = req.getUserId();

        List<BsDayDetail> queryProductResultList = bsDayDetailMapper.selectProjectDayDetail(req.getDayId(),userId);
        List<GetAddDayDetailReq> productList = new ArrayList();
        for (BsDayDetail queryProductResult : queryProductResultList) {
            GetAddDayDetailReq dayMap = new GetAddDayDetailReq();
            //设置用户
            DayUserInfo userInfo = new DayUserInfo();
//        userInfo.setValue(queryProductResult.getUserName());
            userInfo.setGroupId(queryProductResult.getGroupId());
            dayMap.setColid(queryProductResult.getColid());
            //设置工作内容
            dayMap.setWorkContent(queryProductResult.getWorkContent());
            //设置任务
            DayTaskInfo dayTaskInfo = new DayTaskInfo();
            dayTaskInfo.setCode(queryProductResult.getTaskId());
            dayTaskInfo.setValue(queryProductResult.getTaskName());
            dayMap.setUserId(queryProductResult.getCreateBy());
            dayMap.setDayId(queryProductResult.getDayId());
            dayMap.setEditSet(false);
            dayMap.setTaskInfo(dayTaskInfo);
            DayOfficeSpaceInfo officeSpaceInfo = new DayOfficeSpaceInfo();
            officeSpaceInfo.setCode(queryProductResult.getOfficeSpace());
            officeSpaceInfo.setValue(queryProductResult.getOfficeSpaceName());
            dayMap.setOfficeSpaceInfo(officeSpaceInfo);
            dayMap.setDeliverables(queryProductResult.getDeliverables());
            dayMap.setLaborHour(queryProductResult.getLaborHour());
            dayMap.setRejectReason(queryProductResult.getRejectReason());
            dayMap.setStatesFlag(queryProductResult.getStatesFlag());
            DayTaskTypeInfo dayTaskTypeInfo = new DayTaskTypeInfo();
            dayTaskTypeInfo.setCode(queryProductResult.getTaskType());
            dayTaskTypeInfo.setValue(queryProductResult.getTaskTypeName());
            dayMap.setTaskTypeInfo(dayTaskTypeInfo);
            productList.add(dayMap);
        }
        resp.setProductDayDetailList(productList);
        return resp;
    }

    //删除日报
    @Override
    @Transactional
    public void deleteDayInfo(GetDeleteDayReq req) throws Exception {
        //删除日报中的一条
        String userId = req.getUserId();
        String groupId = req.getGroupId();
        BsDayDetailExample bsDayDetailExample1 = new BsDayDetailExample();
        BsDayDetailExample.Criteria criteria1 = bsDayDetailExample1.createCriteria();
        criteria1.andDayidEqualTo(req.getDayId());
        criteria1.andColidEqualTo(req.getColid());
        criteria1.andCreateByEqualTo(userId);
        bsDayDetailMapper.deleteByExample(bsDayDetailExample1);
        }


    public String filterWorkContent(String workContent){
        String newWorkContent = (Pattern.compile(("^[0-9]+[、]|[0-9]+[.]|[0-9]+[\\\\]"),Pattern.MULTILINE)).matcher(workContent).replaceAll("");
        return newWorkContent;
    }

    private String sysDateTime(){
        Date date = new Date();
        DateFormat dataFormat = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
        String createDate = dataFormat.format(date);
        return createDate;
    }
}
